package com.fysports.web.controller.system;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.fysports.framework.kit.sql.Conditions;
import com.fysports.web.controller.AbstractBaseController;
import com.fysports.web.kit.commons.PathBizKit;
import com.fysports.web.kit.commons.StringKit;
import com.fysports.web.model.system.SystemLog;
import com.fysports.web.module.core.DefaultModuleBuilder;
import com.fysports.web.module.core.IModuleFactory;
import com.fysports.web.module.view.vo.BaseWebVO;


/**
 * 系统日志管理工具
 * Created by konbluesky
 * Date : 14-9-17 下午7:42
 * Project : JdmData_WebServer
 */
public class ManagerLogController extends AbstractBaseController {

    IModuleFactory moduleFactory = new DefaultModuleBuilder();


    public void index() {
        LogVO lv = new LogVO(this);
        moduleFactory.create(this);
        execute();
        Page<SystemLog> list = SystemLog.dao
                .searchPaginate(lv.getPageIndex(), pageSize, lv.getConditions(), SystemLog.COL_CREATETIME,
                        "desc");

        setAttr("pageList", list);
        setBaseWebVO(lv);
        render(PathBizKit.getPageAdminPath("system/log/log_list.ftl"));
    }


    public void search() {
        index();
    }


    class LogVO extends BaseWebVO {
        private String useruuid;
        private String username;
        private String laiyuan;


        LogVO(Controller ctr) {
            super(ctr);
        }


        @Override public String toParams() {
            StringBuilder sb = new StringBuilder("?");
            sb.append("useruuid=").append(useruuid).append("&");
            sb.append("username=").append(encode(username)).append("&");
            sb.append("laiyuan=").append(laiyuan);
            return sb.toString();
        }


        @Override public void initParamsReq(Controller ctr) {
            this.laiyuan = ctr.getPara("laiyuan", "");
            this.useruuid = ctr.getPara("useruuid", "");
            this.username = decode(ctr.getPara("username", ""));
        }


        @Override public void setQueryData(Controller ctr) {
            ctr.setAttr("useruuid", useruuid);
            ctr.setAttr("username", username);
            ctr.setAttr("laiyuan", laiyuan);
        }


        @Override public Conditions getConditions() {
            Conditions con = new Conditions().setExcludeField(SystemLog.COL_UUID);
            SystemLog sl = new SystemLog();
            //            if(StringKit.notBlank(useruuid)) {
            //                con.setValueQuery(Conditions.EQUAL, "useruuid", useruuid);
            //                sl.set(SystemLog.COL_USERUUID,useruuid);
            //            }
            if (StringKit.notBlank(laiyuan)) {
                con.setValueQuery(Conditions.FUZZY, "useragent", laiyuan);
                sl.set(SystemLog.COL_USERAGENT, laiyuan);
            }
            con.modelToCondition(sl);
            /* 查出用户 name */
            con.setSelector("select *");
            //            con.setSelector("select * ");
            return con;
        }
    }

}
